package com.ymx.darling.generater.config;

import com.ymx.darling.exception.DarlingException;
import com.ymx.darling.generater.*;
import com.ymx.darling.generater.def.EntityDefConfig;
import com.ymx.darling.generater.table.Table;
import com.ymx.darling.generater.table.TableUtil;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

/**
 * 代码生成器配置
 * @author 爱java的小于
 */
public class GenerateConfig {
    private final GlobalConfig globalConfig = new GlobalConfig();
    private final StrategyConfig strategyConfig = new StrategyConfig();

    /**
     * 设置全局配置
     * @param globalConfig 全局配置实例
     * @return GenerateConfig
     */
    public GenerateConfig globalConfig(Consumer<GlobalConfig> globalConfig) {
        globalConfig.accept(this.globalConfig);
        return this;
    }

    /**
     * 设置策略配置
     * @param strategyConfig 策略配置实例
     * @return GenerateConfig
     */
    public GenerateConfig strategyConfig(UnaryOperator<StrategyConfig> strategyConfig) {
        strategyConfig.apply(this.strategyConfig);
        return this;
    }

    public void start() {
        // 检查配置
        this.requireSettingNonNull();
        // 获取所有表信息
        List<Table> tableInfoList = TableUtil.getTableInfoList(globalConfig);
        // 判断忽略表前缀
        if (strategyConfig.getIgnoreTablePrefix() != null) {
            tableInfoList.forEach(table -> {
                if (table.getName().startsWith(strategyConfig.getIgnoreTablePrefix())) {
                    // 为table中的名称去掉制定前缀
                    table.setName(table.getName().substring(strategyConfig.getIgnoreTablePrefix().length()));
                }
            });
        }
        // 生成实体类
        if (!strategyConfig.isIgnoreEntity()) {
            System.out.println(">>>>>>>>>开始生成实体类...");
            new EntityGenerator(globalConfig, strategyConfig).createEntity(tableInfoList);
        }
        // 生成Mapper
        if (!strategyConfig.isIgnoreMapper()) {
            System.out.println(">>>>>>>>>开始生成Mapper...");
            new MapperGenerator(globalConfig, strategyConfig).createMapper(tableInfoList);
        }
        // 生成Service及其实现类
        if (!strategyConfig.isIgnoreService()) {
            System.out.println(">>>>>>>>>开始生成Service...");
            new ServiceGenerator(globalConfig, strategyConfig).createService(tableInfoList);
        }
    }


    /**
     * 检查所有配置属性不能为空
     */
    private void requireSettingNonNull() {
        String property = null;
        GlobalConfig globalConfig = this.globalConfig;

        if (globalConfig.getProjectRoot() == null) {
            property = "projectPath";
        } else if (globalConfig.getBasePackage() == null) {
            property = "basePackage";
        } else if (globalConfig.getDriver() == null) {
            property = "driver";
        } else if (globalConfig.getUrl() == null) {
            property = "url";
        } else if (globalConfig.getPassword() == null) {
            property = "password";
        } else if (globalConfig.getUsername() == null) {
            property = "username";
        }

        if (property != null) {
            throw new DarlingException("代码生成器缺少配置:"+property);
        }
    }

}
